
import java.io.*;
import java.util.*;
import java.text.*;

public class GrayCodeChromo extends BinaryChromo
{
   // Returns the integer approximation in Binary
   public double[] getValueVector()
   {
      double[] values = new double[Parameters.numGenes];
      for (int i=0; i<Parameters.numGenes; i++)
         values[i] = toBinaryFromGrayCode(getPosIntGeneValue(i));
      return values;
   }

   // We use reflected binary grey code
   protected int toBinaryFromGrayCode(int grayCode)
   {
      // First convert to grey code
      int result = grayCode ^ (grayCode >> 16);
      result ^= (result>>8);
      result ^= (result>>4);
      result ^= (result>>2);
      result ^= (result>>1);

      // Now that we are in binary see if the negabit is on
      int negMask = 1<<(Parameters.geneSize-1);
      if ((result&negMask) == 0)
      {
         // Oh no it is in better go convert it
         result -= negMask;
         result -= negMask;
      }

      // Return our formatted result
      return result;
   }
}
